package stego.StegoCaller;

import java.awt.Color;
import java.io.*;
import javax.swing.JTextPane;
import javax.swing.text.*;
import stego.Drivers;

public class Caller_DownloadContent
{

    private static final String strMyClassName = "caller_DownloadContent";

    public Caller_DownloadContent(File pathTo_JAR, File pathToDownloadFiles, String userName, String password, JTextPane jtextPaneOutput, boolean useNativeAPI, boolean useCustInstr, 
            boolean enterExtCmds, String extCmds)
    {
        processCommand(pathTo_JAR, pathToDownloadFiles, userName, password, jtextPaneOutput, useNativeAPI, useCustInstr, enterExtCmds, extCmds);
    }

    public boolean processCommand(File flePathTo_JAR, File flePathTo_Download, String usrName, String pwd, JTextPane jtxtpneOutput, boolean useNativeInstructions, boolean useCustInstructions, 
            boolean enterExternalCommands, String externalCmds)
    {
        try
        {
            if(useNativeInstructions)
            {
                processDownload_UseNativeInstructions(flePathTo_JAR, flePathTo_Download, usrName, pwd, jtxtpneOutput, false, null);
            } else
            if(useCustInstructions)
            {
                processDownload_UseNativeInstructions(flePathTo_JAR, flePathTo_Download, usrName, pwd, jtxtpneOutput, false, null);
            } else
            if(enterExternalCommands)
            {
                processDownload_UseNativeInstructions(flePathTo_JAR, flePathTo_Download, usrName, pwd, jtxtpneOutput, enterExternalCommands, externalCmds);
            }
        }
        catch(Exception e)
        {
            Drivers.eop("processCommand", "caller_DownloadContent", e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }

    private boolean processDownload_UseNativeInstructions(File flePathToJAR, File flePathToDownloadContent, String strUserName, String strPassword, JTextPane jtxtpneOut, boolean useExternalCommand, String strExternalCommand)
    {
        try
        {
            String strJAR_Name = flePathToJAR.getCanonicalPath().substring(flePathToJAR.getCanonicalPath().lastIndexOf(System.getProperty("file.separator")) + 1);
            String strCommand = (new StringBuilder("java -jar \"")).append(flePathToJAR.getCanonicalPath()).append("\"").append(" -d ").append(strUserName).append(" ").append("\"").append(flePathToDownloadContent.getAbsolutePath()).append("\"").toString();
            if(useExternalCommand && strExternalCommand != null)
            {
                strCommand = strExternalCommand;
            }
            if(!strCommand.endsWith(System.getProperty("file.separator")))
            {
                strCommand = (new StringBuilder(String.valueOf(strCommand))).append(System.getProperty("file.separator")).toString();
            }
            if(strCommand != null && !strCommand.trim().toUpperCase().startsWith("JAVA"))
            {
                Drivers.jop_Warning((new StringBuilder("WARNING: The following command: ")).append(strCommand).append("\nDoes not appear to be within expected parameters to launch a JAR file").toString(), "Unverified Command Received");
            }
            appendTextToJTextPane(jtxtpneOut, (new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
            Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
            Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
            for(String line = ""; (line = input.readLine()) != null;)
            {
                Drivers.sop(line);
                appendTextToJTextPane(jtxtpneOut, line, true);
                Drivers.sop(line);
            }

        }
        catch(Exception e)
        {
            Drivers.eop("processDownload_UseNativeInstructions", "caller_DownloadContent", e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }

    private boolean appendTextToJTextPane(JTextPane jtxtPneToAppend, String strToAppend, boolean insertCarriageReturnAtEndOfString)
    {
        try
        {
            if(insertCarriageReturnAtEndOfString)
            {
                strToAppend = (new StringBuilder(String.valueOf(strToAppend))).append("\n").toString();
            }
            StyledDocument doc = jtxtPneToAppend.getStyledDocument();
            SimpleAttributeSet attribute = new SimpleAttributeSet();
            StyleConstants.setForeground(attribute, Color.black);
            StyleConstants.setBackground(attribute, Color.white);
            StyleConstants.setBold(attribute, false);
            doc.insertString(doc.getLength(), strToAppend, attribute);
        }
        catch(Exception e)
        {
            Drivers.eop("appendTextToJTextPane", strToAppend, e, e.getLocalizedMessage(), true);
            return false;
        }
        return true;
    }
}
